#####################################################################
# Do Personality Traits Predict Voter Attitudes When Politics Is    #
# Structured Around Conflict? Lessons From Israel                   #
#                                                                   #
# Rivlin-Angert, Yakter, and Sheffer                                #
# Public Opinion Quarterly                                          #
#                                                                   #
# Replication code - Figure 1                                       #
#####################################################################
# To load the data, users should place the relevant csv	file in     #
# in their R working directory (or type the full file paths as      #
# saved on their computer).                                         #
#####################################################################

## Required packages
library(foreign)
library(dplyr)
library(ggplot2)

## set-up data
# load data (data created from coefficients extracted from the models)
dat <- read.csv("Angert-Rivlin, Yakter, Sheffer POQ - figure 1 data.csv")
# re-order factor levels so they appear in desired order in the graph
dat$model <- factor(dat$model,levels=c("lr","conflict","social","economic"))
dat$trait <- factor(dat$trait, level=c("agree","consc", "stab","extra","open"))

## plot
# create facet labels for subtitles
labeller_bf <- labeller(trait = c(agree = "Agreeableness", consc = "Conscientiousness",
                                  stab = "Stability", extra = "Extraversion", open = "Openness"))
# plot coefficients (Figure 1)
plot.f1 <- ggplot(dat, aes(x = model, y = coef, color = model, shape = model)) +
  geom_hline(yintercept = 0, colour = gray(0.5),lty = 2) +
  geom_linerange(aes(ymin = lwr90,ymax =upr90), lwd = 1, position = position_dodge(width = 0.5)) +
  geom_pointrange(aes(ymin = lwr95,ymax=upr95), lwd = 0.5, position = position_dodge(width = 0.5)) +
  facet_grid(. ~ trait, labeller = labeller_bf) + 
  scale_color_discrete(name = "Dependent Variable:",labels=c("Left-Right","Conflict","Social","Economic")) +
  scale_shape_discrete(name = "Dependent Variable:",labels=c("Left-Right","Conflict","Social","Economic")) +
  labs(y = "Coefficient") +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    axis.text.x = element_blank(),
    axis.title.x = element_blank(),
    legend.position="bottom",
  )

# save file
ggsave(plot=plot.f1, "Figure 1.jpg", width=6.5, height=3, dpi=900)
